<?php
class CP_OnlineSupportsController extends XPHP_Controller
{	
/**
	 * Phương thức sẽ thực thi trước khi thực thi sau khi controller được khởi tạo
	 */
	public function _init()
	{
		//Load layout
		$this->loadLayout("Administrator");
		//Xác thực
		$this->helper->auth();
	}
	
	public function indexAction()
	{
		$model = new Areas_CP_Models_OnlineSupports();
		$arrSupports = $model->select_all();
		$this->view->arrSupports = $arrSupports;

		return $this->view();
	}
	
	/**
	 * [[
	 * model: Areas_CP_Models_OnlineSupports
	 * ]]
	 */
	public function createAction()
	{
		//Lấy danh sách tỉnh
		$modelProvinces = new Areas_CP_Models_Provinces();
		$arrProvinces = $modelProvinces->select_all();
		$this->view->arrProvinces = $arrProvinces;

		return $this->view();
	}
	
	/**
	 * [[
	 * model: Areas_CP_Models_OnlineSupports
	 * ]]
	 */
	public function createPOST()
	{
		if ($this->hasModel())
		{
			if ($this->model->isValid())
			{
				//Kiểm tra hỗ trợ đã có chưa
				$model = new Areas_CP_Models_OnlineSupports();
				$data = $model->select("name='".$this->model->name."'");
				if (!empty($data))
				{
					//Thông báo hỗ trợ đã tồn tại
				}
				else 
				{
					$this->model->insert();
					//Redirect tới action index
					XPHP_Url::redirectAction($this->url->action("index"));
				}
			}
		}
	}

	public function editAction($args)
	{
		//Lấy danh sách tỉnh
		$modelProvinces = new Areas_CP_Models_Provinces();
		$arrProvinces = $modelProvinces->select_all();
		$this->view->arrProvinces = $arrProvinces;
		
		$model = new Areas_CP_Models_OnlineSupports($args[0]);

		return $this->view($model);
	}
	
	/**
	 * [[
	 * model: Areas_CP_Models_OnlineSupports
	 * ]]
	 */
	public function editPOST()
	{
		if ($this->hasModel())
		{
			if ($this->model->isValid())
			{
				$this->model->update();
			}
		}
	}
	
	public function deleteAction($args)
	{
		$model = new Areas_CP_Models_OnlineSupports($args[0]);
		$model->delete();
		//Redirect tới trang index
		XPHP_Url::redirectUrl($this->url->action("index"));
	}
}